package com.demo.rabbitmq.demo.routing;

import com.demo.rabbitmq.util.RabbitMQUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    //交换机名称
    static final String DIRECT_EXCHAGE = "direct_exchange";
    //列队名称
    static final String DIRECT_OURUE_INSERT = "direct_queue_insert";
    static final String DIRECT_OURUE2_UPDATE = "direct_queue_update";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMQUtil.getConnection();

        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare(DIRECT_EXCHAGE, BuiltinExchangeType.DIRECT);
        //声明创建列队
        channel.queueDeclare(DIRECT_OURUE_INSERT,true,false,false,null);
        channel.queueDeclare(DIRECT_OURUE2_UPDATE,true,false,false,null);
        //列队绑定到交换机 并指定路由（instet update）
        channel.queueBind(DIRECT_OURUE_INSERT, DIRECT_EXCHAGE, "insert");

        channel.queueBind(DIRECT_OURUE2_UPDATE, DIRECT_EXCHAGE, "update");
        channel.queueBind(DIRECT_OURUE2_UPDATE, DIRECT_EXCHAGE, "delete");
        for(int i=1;i<=30;i++){
            String messageInsert="insert消息："+i;
            String messageUpdate="update消息："+i;
            String messageUpdate_del="update消息 删除："+i;
            System.out.println(messageInsert);
            System.out.println(messageUpdate);
            channel.basicPublish(DIRECT_EXCHAGE,"insert",null,messageInsert.getBytes());

            channel.basicPublish(DIRECT_EXCHAGE,"update",null,messageUpdate.getBytes());
            channel.basicPublish(DIRECT_EXCHAGE,"delete",null,messageUpdate_del.getBytes());
        }
        channel.close();
        connection.close();


    }
}
